﻿// -----------------------------------------------------------------------
// <copyright file="NativeMethods.cs" company="Microsoft">
//  Yeah right
// </copyright>
// -----------------------------------------------------------------------

namespace Eeeek
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime.InteropServices;

    /// <summary>
    /// Holds code for native (User32.dll) methods
    /// </summary>
    [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "Reviewed. Suppression is OK here.")]
    public static class NativeMethods
    {
        #region Fields

        /// <summary>
        /// Set the window state to normal
        /// </summary>
        internal const int SW_SHOWNORMAL = 1;

        /// <summary>
        /// Maximise the window
        /// </summary>
        internal const int SW_SHOWMAXIMIZED = 3;

        /// <summary>
        /// Set the window state to restored
        /// </summary>
        internal const int SW_RESTORE = 9;

        /// <summary>
        /// User32.dll code for mouseMove event
        /// </summary>
        // ReSharper disable InconsistentNaming
        internal const int MOUSEEVENTF_MOVE = 0x0001; /* mouse move */

        /// <summary>
        /// User32.dll code for left button down event
        /// </summary>
        internal const int MOUSEEVENTF_LEFTDOWN = 0x0002; /* left button down */

        /// <summary>
        /// User32.dll code for left button up event
        /// </summary>
        internal const int MOUSEEVENTF_LEFTUP = 0x0004; /* left button up */

        /// <summary>
        /// User32.dll code for right button down event
        /// </summary>
        internal const int MOUSEEVENTF_RIGHTDOWN = 0x0008; /* right button down */

        /// <summary>
        /// User32.dll code for right button up event
        /// </summary>
        internal const int MOUSEEVENTF_RIGHTUP = 0x0010; /* right button up */

        /// <summary>
        /// User32.dll code for middle button down event
        /// </summary>
        internal const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; /* middle button down */

        /// <summary>
        /// User32.dll code for middle button up event
        /// </summary>
        internal const int MOUSEEVENTF_MIDDLEUP = 0x0040; /* middle button up */

        /// <summary>
        /// User32.dll code for X button down
        /// </summary>
        internal const int MOUSEEVENTF_XDOWN = 0x0080; /* x button down */

        /// <summary>
        /// User32.dll code for x button up event
        /// </summary>
        // ReSharper disable InconsistentNaming
        internal const int MOUSEEVENTF_XUP = 0x0100; /* x button up */
        // ReSharper restore InconsistentNaming

        /// <summary>
        /// User32.dll code for mouse wheel event
        /// </summary>
        internal const int MOUSEEVENTF_WHEEL = 0x0800; /* wheel button rolled */

        /// <summary>
        /// User32.dll code for enetr virtual desktop event
        /// </summary>
        internal const int MOUSEEVENTF_VIRTUALDESK = 0x4000; /* map to entire virtual desktop */

        /// <summary>
        /// User32.dll code for absolute mouse move event
        /// </summary>
        internal const int MOUSEEVENTF_ABSOLUTE = 0x8000; /* absolute move */
        // ReSharper restore InconsistentNaming

        /// <summary>
        /// The code for the 1 key
        /// </summary>
        private const byte OneKeyChar = 0x31;

        /// <summary>
        /// The code for the 2 key
        /// </summary>
        private const byte TwoKeyChar = 0x32;

        /// <summary>
        /// The code for the 3 key
        /// </summary>
        private const byte ThreeKeyChar = 0x33;

        /// <summary>
        /// the code for the 4 key
        /// </summary>
        private const byte FourKeyChar = 0x34;

        /// <summary>
        /// The code for the 5 key
        /// </summary>
        private const byte FiveKeyChar = 0x35;

        /// <summary>
        /// The code for the 6 key
        /// </summary>
        private const byte SixKeyChar = 0x36;

        /// <summary>
        /// The code for the 7 key
        /// </summary>
        private const byte SevenKeyChar = 0x37;

        /// <summary>
        /// The code for the 8 key
        /// </summary>
        private const byte EightKeyChar = 0x38;

        /// <summary>
        /// the code for the 9 key
        /// </summary>
        private const byte NineKeyChar = 0x39;

        /// <summary>
        /// The code for the 0 key
        /// </summary>
        private const byte ZeroKeyChar = 0x30;

        /// <summary>
        /// The code for  the - key
        /// </summary>
        private const byte MinusKeyChar = 0xBD;

        /// <summary>
        /// The code for the = key
        /// </summary>
        private const byte EqualsKeyChar = 0xBB;

        #endregion

        /// <summary>
        /// Causes a keyboard event
        /// </summary>
        /// <param name="bVk">The b vk.</param>
        /// <param name="bScan">The b scan.</param>
        /// <param name="dwFlags">The dw flags.</param>
        /// <param name="dwExtraInfo">The dw extra info.</param>
        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

        /// <summary>
        /// Mouse_events the specified dw flags.
        /// </summary>
        /// <param name="dwFlags">The dw flags.</param>
        /// <param name="dx">The dx.</param>
        /// <param name="dy">The dy.</param>
        /// <param name="cButtons">The c buttons.</param>
        /// <param name="dwExtraInfo">The dw extra info.</param>
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        /// <summary>
        /// Sets the cursor pos.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <returns>Apparently an int</returns>
        [DllImport("user32")]
        public static extern int SetCursorPos(int x, int y);

        /// <summary>
        /// Finds the specified window's handle.
        /// </summary>
        /// <param name="className">Name of the class.</param>
        /// <param name="windowName">Name of the window.</param>
        /// <returns>An int, apparently</returns>
        [DllImport("User32.dll")]
        public static extern int FindWindow(string className, string windowName);

        /// <summary>
        /// Sets the foreground window.
        /// </summary>
        /// <param name="hWnd">The window handle</param>
        /// <returns>An int, apparently</returns>
        [DllImport("User32.dll")]
        public static extern int SetForegroundWindow(int hWnd);

        /// <summary>
        /// Registers the hot key.
        /// </summary>
        /// <param name="hwnd">The HWND.</param>
        /// <param name="id">The id.</param>
        /// <param name="fsModifiers">The fs modifiers.</param>
        /// <param name="vk">The vk.</param>
        /// <returns>True or False</returns>
        [DllImport("user32", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RegisterHotKey(IntPtr hwnd, int id, uint fsModifiers, uint vk);

        /// <summary>
        /// Unregisters the hot key.
        /// </summary>
        /// <param name="hwnd">The HWND.</param>
        /// <param name="id">The id.</param>
        /// <returns>1 or 0</returns>
        [DllImport("user32", SetLastError = true)]
        public static extern int UnregisterHotKey(IntPtr hwnd, int id);

        /// <summary>
        /// Globals the add atom.
        /// </summary>
        /// <param name="lpString">The lp string.</param>
        /// <returns>A 16 bit int</returns>
        [DllImport("kernel32", SetLastError = true)]
        public static extern short GlobalAddAtom(string lpString);

        /// <summary>
        /// Globals the delete atom.
        /// </summary>
        /// <param name="nAtom">The n atom.</param>
        /// <returns>a 16 bit int</returns>
        [DllImport("kernel32", SetLastError = true)]
        public static extern short GlobalDeleteAtom(short nAtom);

        /// <summary>
        /// Shows the window.
        /// </summary>
        /// <param name="hWnd">The window handle.</param>
        /// <param name="nCmdShow">The show action.</param>
        /// <returns>a boolean success of failure</returns>
        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 
    }
}